#include "expirycheck.h"
#include "ui_expirycheck.h"
#include <QMessageBox>
#include <QJsonArray>
#include <QStatusBar>
#include "mainwindow.h"

ExpiryCheckWindow::ExpiryCheckWindow(QWidget *parent)
    : QWidget(parent)
    , expirycheckwindowUi(new Ui::ExpiryCheckWindow)
    , networkManager(new NetworkManager(this))
{
    expirycheckwindowUi->setupUi(this);
    connect(networkManager, &NetworkManager::checkExpiryResponseReceived, this, &ExpiryCheckWindow::handleCheckExpiryResponse);
}

ExpiryCheckWindow::~ExpiryCheckWindow()
{
    delete expirycheckwindowUi;
}

void ExpiryCheckWindow::handleCheckExpiryResponse(const QJsonObject &response) {
    int code = response.value("code").toInt();
    qDebug() << response;
    if (code == 200) {
        expirycheckwindowUi->alreadyExpiryTable->setRowCount(0);
        expirycheckwindowUi->soonToExpiryTable->setRowCount(0);

        QString dataString = response.value("data").toString();
        QJsonDocument dataDoc = QJsonDocument::fromJson(dataString.toUtf8());
        QJsonObject dataObject = dataDoc.object();

        if (dataObject.isEmpty()) {
            MainWindow::getMainWindow()->statusBar()->showMessage("暂无任何过期或即将过期的药品库存。", 3000);
            return;
        }

        QJsonArray alreadyExpiredArray = dataObject.value("already_expired").toArray();
        qDebug() << alreadyExpiredArray;
        for (const QJsonValue &value : alreadyExpiredArray) {
            QJsonObject obj = value.toObject();
            int rowCount = expirycheckwindowUi->alreadyExpiryTable->rowCount();
            expirycheckwindowUi->alreadyExpiryTable->insertRow(rowCount);

            expirycheckwindowUi->alreadyExpiryTable->setItem(rowCount, 0, new QTableWidgetItem(obj.value("name").toString()));
            expirycheckwindowUi->alreadyExpiryTable->setItem(rowCount, 1, new QTableWidgetItem(obj.value("category").toString()));
            expirycheckwindowUi->alreadyExpiryTable->setItem(rowCount, 2, new QTableWidgetItem(obj.value("supplier").toString()));
            expirycheckwindowUi->alreadyExpiryTable->setItem(rowCount, 3, new QTableWidgetItem(QString::number(obj.value("price").toDouble())));
            expirycheckwindowUi->alreadyExpiryTable->setItem(rowCount, 4, new QTableWidgetItem(QString::number(obj.value("stock").toInt())));
            expirycheckwindowUi->alreadyExpiryTable->setItem(rowCount, 5, new QTableWidgetItem(obj.value("purchase_date").toString()));
            expirycheckwindowUi->alreadyExpiryTable->setItem(rowCount, 6, new QTableWidgetItem(obj.value("expired_date").toString()));
        }

        QJsonArray soonToExpireArray = dataObject.value("soon_to_expire").toArray();
        qDebug() << soonToExpireArray;
        for (const QJsonValue &value : soonToExpireArray) {
            QJsonObject obj = value.toObject();
            int rowCount = expirycheckwindowUi->soonToExpiryTable->rowCount();
            expirycheckwindowUi->soonToExpiryTable->insertRow(rowCount);

            expirycheckwindowUi->soonToExpiryTable->setItem(rowCount, 0, new QTableWidgetItem(obj.value("name").toString()));
            expirycheckwindowUi->soonToExpiryTable->setItem(rowCount, 1, new QTableWidgetItem(obj.value("category").toString()));
            expirycheckwindowUi->soonToExpiryTable->setItem(rowCount, 2, new QTableWidgetItem(obj.value("supplier").toString()));
            expirycheckwindowUi->soonToExpiryTable->setItem(rowCount, 3, new QTableWidgetItem(QString::number(obj.value("price").toDouble())));
            expirycheckwindowUi->soonToExpiryTable->setItem(rowCount, 4, new QTableWidgetItem(QString::number(obj.value("stock").toInt())));
            expirycheckwindowUi->soonToExpiryTable->setItem(rowCount, 5, new QTableWidgetItem(obj.value("purchase_date").toString()));
            expirycheckwindowUi->soonToExpiryTable->setItem(rowCount, 6, new QTableWidgetItem(obj.value("expired_date").toString()));
        }

    }
    else {
        QString desc = response.value("desc").toString();
        QMessageBox::warning(this, "获取失败", desc);
    }
}

void ExpiryCheckWindow::on_checkButton_clicked()
{
    networkManager->checkExpiry();
}
